package com.yiqiu.system.controller;

import com.yiqiu.common.core.domain.entity.SysUser;
import com.yiqiu.common.core.domain.vo.AjaxResult;
import com.yiqiu.common.core.util.HttpUtils;
import com.yiqiu.system.mapper.SysUserMapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author Joeywei
 * @version 1.0
 * @date 2024/11/20.
 */
@RestController
public class TestController {


    @GetMapping("/test1")
    public AjaxResult version2() {
//        StringUtils.subString(null,1,2);
        return AjaxResult.success("  3333 222 111    ".trim());
    }

    @GetMapping("/test2")
    public AjaxResult test() {
        List<String> list = new ArrayList<>();

        return AjaxResult.success(list);
    }

    private void test222() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("输入信息（姓名，分数） 输入-1结束");
            String s = scanner.nextLine();
            if ("-1".equals(s)) {
                break;
            }
            String[] split = s.split(",");
            if (split.length == 2) {
                System.out.println("输入信息非法");
            }
            int fenshu = Integer.parseInt(split[1]);
            if (fenshu < 0 || fenshu > 100) {
                System.out.println("分数非法");
            }
        }
    }

    @GetMapping("/testHttp")
    public AjaxResult testHttp() {
        String s = HttpUtils.sendGet("http://www.baidu.com", null, null);
        System.out.println(s);
        return AjaxResult.success(s);
    }

    @Resource
    private SysUserMapper sysUserMapper;

    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody SysUser user) {
        sysUserMapper.insertUser(user);
        return AjaxResult.success();
    }

    @DeleteMapping("/del/{id}")
    public AjaxResult delete(@PathVariable("id") Integer id) {
        sysUserMapper.deleteById(id);
        return AjaxResult.success();
    }

    @GetMapping("/list")
    public AjaxResult list() {
        List<SysUser> data = sysUserMapper.selectList(null);
        return AjaxResult.success(data);
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody SysUser user) {
        System.out.println("接收到的用户信息：" + user);
        // 打印用户ID类型
        System.out.println("用户ID类型：" + (user.getUserId() != null ? user.getUserId().getClass().getName() : "null"));
        // 尝试使用其他方式更新
        int rows = sysUserMapper.updateById(user);
        System.out.println("影响的行数：" + rows);
        return AjaxResult.success();
    }
}